package org.taofuxn.framework;


import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.taofuxn.yycreazy.R;

import org.taofuxn.framework.ui.activity.SettingActivity;
import org.taofuxn.framework.ui.base.BaseActivity;
import org.taofuxn.framework.util.AppStyleUtil;

import butterknife.InjectView;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @InjectView(R.id.nav_view)
    NavigationView navigationView;
    @InjectView(R.id.main_viewpage)
    ViewPager mainViewpage;
    @InjectView(R.id.main_tablayout)
    TabLayout mainTabLayout;


    @Override
    protected int setContentLayout() {

        return R.layout.activity_main;
    }

    @Override
    protected void init() {

        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);

        if (AppStyleUtil.curStyle == AppStyleUtil.StyleType.LIGHT)
            navigationView.getMenu().findItem(R.id.nav_switchStype).setTitle("夜间模式");
        else
            navigationView.getMenu().findItem(R.id.nav_switchStype).setTitle("日间模式");

        mainViewpage.setOffscreenPageLimit(0);
        mainViewpage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {

                switch (position) {
                    case 0:
                    case 1:
                    case 2:
                    default:
                        return null;
                }
            }

            @Override
            public int getCount() {
                return 4;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "精选段子";
                    case 1:
                        return "最新趣图";
                    case 2:
                        return "穿越段子";
                    default:
                        return "穿越图片";
                }
            }
        });

        mainTabLayout.setupWithViewPager(mainViewpage);
    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        if (id == R.id.nav_mycollection) {
        } else if (id == R.id.nav_zhuangbi) {

        } else if (id == R.id.nav_vchatnews) {

        } else if (id == R.id.nav_share) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.setType("text/*");
            sendIntent.putExtra(Intent.EXTRA_TEXT, "轻松一刻");
            startActivity(sendIntent);
        } else if (id == R.id.nav_set) {
            forward(SettingActivity.class);
        } else if (id == R.id.nav_switchStype) {
            //主题更换
            if (AppStyleUtil.curStyle == AppStyleUtil.StyleType.LIGHT) {
               switchThemeTo(AppStyleUtil.StyleType.NIGHT);
            } else {
                switchThemeTo(AppStyleUtil.StyleType.LIGHT);
            }
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}
